<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Vue + Ts 实战 | 北城韩雨</title><meta name="keywords" content="TS"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="TS">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue + Ts 实战">
<meta property="og:url" content="http://example.com/2020/04/24/vue+ts%E5%AE%9E%E6%88%98/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="TS">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790">
<meta property="article:published_time" content="2020-04-23T16:00:00.000Z">
<meta property="article:modified_time" content="2019-06-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="TypeScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2020/04/24/vue+ts%E5%AE%9E%E6%88%98/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Vue + Ts 实战',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-06-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Vue + Ts 实战</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-04-23T16:00:00.000Z" title="发表于 2020-04-24 00:00:00">2020-04-24</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-06-02T16:00:00.000Z" title="更新于 2019-06-03 00:00:00">2019-06-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/">项目实战</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>16分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Vue + Ts 实战"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Vue-TypeScript实战"><a href="#Vue-TypeScript实战" class="headerlink" title="Vue + TypeScript实战"></a>Vue + TypeScript实战</h1><h2 id="基础"><a href="#基础" class="headerlink" title="基础"></a>基础</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>初始化命令 还是使用vue-cli脚手架去搭建 vue create vue-typescript-demo</span><br><span class="line"><span class="number">2.</span>选择的话我们这里就选择了 typescript</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>创建出来的项目 现在都是.ts后缀解为的文件了</span><br><span class="line"><span class="number">2.</span> 多了 shims-tsx.d.ts文件  这里主要用于我们可以些tsx结尾的文件 里面可以写jsx的代码</span><br><span class="line"><span class="number">3.</span> 多了 shims-vue.d.ts文件 用ts可以.vue的文件 </span><br></pre></td></tr></table></figure>
<h2 id="组文件文件里的写法"><a href="#组文件文件里的写法" class="headerlink" title="组文件文件里的写法"></a>组文件文件里的写法</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;home&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">img</span> <span class="attr">alt</span>=<span class="string">&quot;Vue logo&quot;</span> <span class="attr">src</span>=<span class="string">&quot;../assets/logo.png&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">HelloWorld</span> <span class="attr">msg</span>=<span class="string">&quot;Welcome to Your Vue.js + TypeScript App&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line">这里要是使用了lang=&quot;ts&quot;</span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span></span><br><span class="line">    </span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; Component, Vue &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span>;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> HelloWorld <span class="keyword">from</span> <span class="string">&#x27;@/components/HelloWorld.vue&#x27;</span>; <span class="comment">// @ is an alias to /src</span></span></span><br><span class="line"><span class="javascript"><span class="comment">//这里就是一个装饰器的写法 注册了自定义组件</span></span></span><br><span class="line">@Component(&#123;</span><br><span class="line">  components: &#123;</span><br><span class="line">    HelloWorld,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;)</span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Home</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;&#125;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>所以这里再写的话 我们在些script标签的时候 要指定 script标签中的lang=<span class="string">&quot;ts&quot;</span></span><br><span class="line"><span class="number">2.</span><span class="keyword">import</span> &#123; Component, Vue &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span>;  引入了很多装饰器</span><br><span class="line"><span class="number">3.</span>我们所有的组件都是<span class="class"><span class="keyword">class</span>的形式 要继承<span class="title">vue</span> 如下写法</span></span><br><span class="line"><span class="class"><span class="title">export</span> <span class="title">default</span> <span class="title">class</span> <span class="title">Home</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">vue-property-decorator <span class="comment">//装饰器</span></span><br><span class="line">@Component <span class="comment">//用来修饰组件</span></span><br><span class="line">@Watch <span class="comment">//监听</span></span><br><span class="line">@Prop <span class="comment">//自定义组件的时候接收参数</span></span><br><span class="line">@Model <span class="comment">// 传参 v-model</span></span><br><span class="line">@Emit <span class="comment">//向父组件传递回调函数</span></span><br></pre></td></tr></table></figure>
<h2 id="Component装饰器"><a href="#Component装饰器" class="headerlink" title="@Component装饰器"></a>@Component装饰器</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>用来写组件 或者注册自定义组件</span><br><span class="line"><span class="number">2.</span>下面我们用about.vue页面为例</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@Component(&#123;</span><br><span class="line">  name: <span class="string">&#x27;About&#x27;</span>, <span class="comment">//这里就是自己的当前组件的名字 等同于我们原来的 export default &#123; name:&#x27;About&#x27; &#125;</span></span><br><span class="line">  components:&#123;</span><br><span class="line">	<span class="comment">//这里如果还有别的组件 就在这里里面写 如果没有自定义组件 这里也可以不写</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="数据的定义"><a href="#数据的定义" class="headerlink" title="数据的定义"></a>数据的定义</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们以前的数据是定义在data函数里面 </span><br><span class="line"><span class="number">2.</span>ts的项目 数据直接定义到 类里面即可</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;about&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>This is an about page<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; Component, Vue &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span>;</span></span><br><span class="line"></span><br><span class="line">@Component(&#123;</span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;About&#x27;</span>,</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">About</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">    <span class="comment">//数据直接写在类里即可</span></span></span><br><span class="line"><span class="javascript">	firstName:string = <span class="string">&#x27;张三&#x27;</span></span></span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="计算属性的写法"><a href="#计算属性的写法" class="headerlink" title="计算属性的写法"></a>计算属性的写法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>直接定义一个get 修饰符 然后跟着你计算属性的函数即可</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;about&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>This is an about page<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;firstName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;lastName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;FullName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; Component, Vue &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span>;</span></span><br><span class="line"></span><br><span class="line">@Component(&#123;</span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;About&#x27;</span>,</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">About</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">  firstName = <span class="string">&#x27;张&#x27;</span></span></span><br><span class="line"></span><br><span class="line"><span class="javascript">  lastName = <span class="string">&#x27;三&#x27;</span></span></span><br><span class="line"><span class="javascript">  <span class="comment">//计算属性这里直接用get修饰符 后面跟着你计算属性的函数逻辑即可</span></span></span><br><span class="line"><span class="javascript">  <span class="keyword">get</span> <span class="title">FullName</span>() &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.firstName&#125;</span> <span class="subst">$&#123;<span class="built_in">this</span>.lastName&#125;</span>`</span>;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="计算属性的set"><a href="#计算属性的set" class="headerlink" title="计算属性的set"></a>计算属性的set</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们在原来的语法中 计算属性 在获取的时候可以用set </span><br><span class="line">computed:&#123;</span><br><span class="line">    fullName:&#123;</span><br><span class="line">        <span class="function"><span class="title">get</span>(<span class="params"></span>)</span>&#123; <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.firstName&#125;</span> <span class="subst">$&#123;<span class="built_in">this</span>.lastName&#125;</span>`</span>&#125;</span><br><span class="line">        <span class="function"><span class="title">set</span>(<span class="params">vale</span>)</span> &#123;<span class="comment">//这里的value 就是 当你改变计算属性的值的时候 拿到的值&#125;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;about&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>This is an about page<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;firstName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;lastName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;FullName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;modifyFullname&quot;</span>&gt;</span>修改值<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; Component, Vue &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span>;</span></span><br><span class="line"></span><br><span class="line">@Component(&#123;</span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;About&#x27;</span>,</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">About</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">  firstName = <span class="string">&#x27;张&#x27;</span></span></span><br><span class="line"></span><br><span class="line"><span class="javascript">  lastName = <span class="string">&#x27;三&#x27;</span></span></span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="keyword">get</span> <span class="title">FullName</span>() &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.firstName&#125;</span> <span class="subst">$&#123;<span class="built_in">this</span>.lastName&#125;</span>`</span>;</span></span><br><span class="line">  &#125;</span><br><span class="line"><span class="javascript">  <span class="comment">//这由于 我们在点击按钮的时候 对我们的计算出行重新赋值了 而这个set的回调函数里 就是我们重新赋值的值 我们就可以做对应的逻辑</span></span></span><br><span class="line"><span class="javascript">  <span class="keyword">set</span> <span class="title">FullName</span>(<span class="params">val</span>) &#123;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(val);</span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> arr = val.split(<span class="string">&#x27;&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(arr);</span></span><br><span class="line"><span class="javascript">    <span class="comment">// eslint-disable-next-line prefer-destructuring</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">this</span>.firstName = arr[<span class="number">0</span>];</span></span><br><span class="line"><span class="javascript">    <span class="comment">// eslint-disable-next-line prefer-destructuring</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">this</span>.lastName = arr[<span class="number">1</span>];</span></span><br><span class="line">  &#125;</span><br><span class="line"><span class="javascript">  <span class="comment">//点击按钮的时候 把我们的计算属性 重新赋值了  所以就可以触发set 函数 从而在里面做出对应的逻辑</span></span></span><br><span class="line"><span class="javascript">  <span class="comment">//这里 方法 直接写在在类里面即可 不用向我们以前一样要写在 methods里面</span></span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">modifyFullname</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">this</span>.FullName = <span class="string">&#x27;李四&#x27;</span>;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="Watch的写法"><a href="#Watch的写法" class="headerlink" title="@Watch的写法"></a>@Watch的写法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们还是要引入watch 装饰器</span><br><span class="line"><span class="number">2.</span>装饰器下面写你要监听的 比如这里我们监听计算属性 如果改变了 </span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;about&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>This is an about page<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;firstName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;lastName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;FullName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;modifyFullname&quot;</span>&gt;</span>修改值<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; Component, Vue, Watch &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span>;</span></span><br><span class="line"></span><br><span class="line">@Component(&#123;</span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;About&#x27;</span>,</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">About</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">  firstName = <span class="string">&#x27;张&#x27;</span></span></span><br><span class="line"></span><br><span class="line"><span class="javascript">  lastName = <span class="string">&#x27;三&#x27;</span></span></span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="keyword">get</span> <span class="title">FullName</span>() &#123;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">return</span> <span class="string">`<span class="subst">$&#123;<span class="built_in">this</span>.firstName&#125;</span> <span class="subst">$&#123;<span class="built_in">this</span>.lastName&#125;</span>`</span>;</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="keyword">set</span> <span class="title">FullName</span>(<span class="params">val</span>) &#123;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(val);</span></span><br><span class="line"><span class="javascript">    <span class="keyword">const</span> arr = val.split(<span class="string">&#x27;&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(arr);</span></span><br><span class="line"><span class="javascript">    <span class="comment">// eslint-disable-next-line prefer-destructuring</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">this</span>.firstName = arr[<span class="number">0</span>];</span></span><br><span class="line"><span class="javascript">    <span class="comment">// eslint-disable-next-line prefer-destructuring</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">this</span>.lastName = arr[<span class="number">1</span>];</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">modifyFullname</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">this</span>.FullName = <span class="string">&#x27;李四&#x27;</span>;</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="javascript">  <span class="comment">// 还是要写在类里面 装饰下面 写你要监听的数据</span></span></span><br><span class="line"><span class="javascript">  <span class="comment">//这 装饰器里面传入 我们要监听的数据字段 然后就执行我们下面的那段函数 函数名称可以随意</span></span></span><br><span class="line"><span class="javascript">  <span class="comment">//当我们监听 firstName 字段的数据改变的时候 我们就执行 firstNamechange 这段函数里面的逻辑</span></span></span><br><span class="line"><span class="javascript">  @Watch(<span class="string">&#x27;firstName&#x27;</span>)</span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">firstNamechange</span>(<span class="params">newval, oldval</span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(newval, oldval, <span class="string">&#x27;====watch&#x27;</span>);</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="Prop的写法"><a href="#Prop的写法" class="headerlink" title="@Prop的写法"></a>@Prop的写法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>主要用来 接收父组件穿过来的值  一个值我们就要写一个装饰器</span><br><span class="line"><span class="number">2.</span>下面主要通过一个列表的组件 来掩饰 一些用法</span><br></pre></td></tr></table></figure>
<h3 id="父组件的写法"><a href="#父组件的写法" class="headerlink" title="父组件的写法"></a>父组件的写法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>定义了接口 和 传参的类型</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">//首先父组件 我们要定义数据 就是一个数组 数组的每一项有姓名 年龄 性别 </span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;about&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>This is an about page<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;firstName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;lastName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>&#123;&#123;FullName&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;modifyFullname&quot;</span>&gt;</span>修改值<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">hr</span>&gt;</span></span><br><span class="line">     <span class="comment">&lt;!--这里我们开始循环并把每一项都传入我们的User组件里 这里直接是用v-bind 就不用一个一个去写了--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">User</span> <span class="attr">v-for</span>=<span class="string">&quot;item  in userList&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;item.id&quot;</span> <span class="attr">v-bind</span>=<span class="string">&quot;item&quot;</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; Component, Vue&#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span>;</span></span><br><span class="line"><span class="javascript"><span class="comment">//引入我们写好的User组件 里面就是单纯的展示姓名性别的</span></span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> User <span class="keyword">from</span> <span class="string">&#x27;@/components/User.vue&#x27;</span>;</span></span><br><span class="line"><span class="javascript"><span class="comment">//定义接口</span></span></span><br><span class="line">interface Iuser&#123;</span><br><span class="line">  id: number;</span><br><span class="line">  name: string;</span><br><span class="line">  age: number | string;</span><br><span class="line"><span class="javascript">  <span class="comment">//这里性别可传可不传</span></span></span><br><span class="line">  sex?: number | string;</span><br><span class="line">&#125;</span><br><span class="line">@Component(&#123;</span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;About&#x27;</span>,</span></span><br><span class="line"><span class="javascript">  <span class="comment">//在装饰器里注册一下我们的自定义组件</span></span></span><br><span class="line">  components:&#123;</span><br><span class="line">      User</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">About</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">  <span class="comment">//使用接口</span></span></span><br><span class="line"><span class="javascript">  userList: <span class="built_in">Array</span>&lt;Iuser&gt; = [</span></span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span>, <span class="attr">sex</span>: <span class="number">0</span>, <span class="attr">id</span>: <span class="number">1</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">28</span>, <span class="attr">sex</span>: <span class="number">1</span>, <span class="attr">id</span>: <span class="number">2</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;王五&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span>, <span class="attr">sex</span>: <span class="number">0</span>, <span class="attr">id</span>: <span class="number">3</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">  ]</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="User组件的写法"><a href="#User组件的写法" class="headerlink" title="User组件的写法"></a>User组件的写法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这个组件我们主要用于接收并展示父组件穿过来的数据</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">        姓名：&#123;&#123;name&#125;&#125;,</span><br><span class="line">        年龄：&#123;&#123;age&#125;&#125;,</span><br><span class="line">        <span class="comment">&lt;!--使用枚举 把我们的性别翻译出来--&gt;</span></span><br><span class="line">        性别：&#123;&#123;Sex[sex]&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="comment">//定义一个枚举</span></span></span><br><span class="line">enum SEX &#123;</span><br><span class="line"><span class="javascript">  <span class="string">&#x27;男&#x27;</span> = <span class="number">1</span>,</span></span><br><span class="line"><span class="javascript">  <span class="string">&#x27;女&#x27;</span> = <span class="number">0</span></span></span><br><span class="line">&#125;</span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; Prop, Vue, Component &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span>;</span></span><br><span class="line"><span class="javascript"><span class="comment">//这里记得一定要加上名字 否则有的时候会报错</span></span></span><br><span class="line">@Component(&#123;</span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;User&#x27;</span>,</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;</span></span><br><span class="line"><span class="javascript"><span class="comment">//在定义一个数据 让它等于我们的枚举 这样上面我们性别在翻译的时候 即可使用枚举了</span></span></span><br><span class="line">Sex:any = SEX</span><br><span class="line"><span class="javascript"><span class="comment">// 这里就是要告诉装饰器你给我穿过来的是什么类型 类似我们以前的写法 prop:&#123;id:&#123;type:Number&#125;&#125;</span></span></span><br><span class="line"><span class="javascript">@Prop(<span class="built_in">Number</span>)</span></span><br><span class="line"><span class="javascript"><span class="comment">// 加个！就是非空断言 必须要传</span></span></span><br><span class="line">id!: number</span><br><span class="line"></span><br><span class="line"><span class="javascript">@Prop(<span class="built_in">String</span>)</span></span><br><span class="line">name!: string</span><br><span class="line"></span><br><span class="line"><span class="javascript">@Prop(&#123; <span class="attr">type</span>: <span class="built_in">Number</span> &#125;)</span></span><br><span class="line">age!: number</span><br><span class="line"><span class="javascript"><span class="comment">//这里也可以设置默认值</span></span></span><br><span class="line"><span class="javascript">@Prop(&#123; <span class="attr">type</span>: <span class="built_in">Number</span>, <span class="attr">default</span>: <span class="number">1</span> &#125;)</span></span><br><span class="line">sex!: number</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="Emit的写法"><a href="#Emit的写法" class="headerlink" title="@Emit的写法"></a>@Emit的写法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>当我们需要子组件向父组件抛事件的时候 要引入Emit</span><br><span class="line"><span class="number">2.</span>默认的参数 就是你@Emit装饰器装饰的方法 里 你传递的参数</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">        姓名：&#123;&#123;name&#125;&#125;,</span><br><span class="line">        年龄：&#123;&#123;age&#125;&#125;,</span><br><span class="line">        性别：&#123;&#123;Sex[sex]&#125;&#125;</span><br><span class="line">        <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;remove(id)&quot;</span>&gt;</span>删除<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123;</span></span><br><span class="line">  Prop, Vue, Component, Emit,</span><br><span class="line"><span class="javascript">&#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span>;</span></span><br><span class="line"></span><br><span class="line">enum SEX &#123;</span><br><span class="line"><span class="javascript">    <span class="string">&#x27;男&#x27;</span> = <span class="number">1</span>,</span></span><br><span class="line"><span class="javascript">    <span class="string">&#x27;女&#x27;</span> = <span class="number">0</span></span></span><br><span class="line">&#125;</span><br><span class="line">@Component(&#123;</span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;User&#x27;</span>,</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;</span></span><br><span class="line"><span class="javascript"><span class="comment">// 这里就是要告诉装饰器你给我穿过来的是什么类型 类似我们以前的写法 prop:&#123;id:&#123;type:Number&#125;&#125;</span></span></span><br><span class="line"><span class="javascript">@Prop(<span class="built_in">Number</span>)</span></span><br><span class="line"><span class="javascript"><span class="comment">// 加个！就是非空断言 必须要传</span></span></span><br><span class="line">id!: number</span><br><span class="line"></span><br><span class="line"><span class="javascript">@Prop(<span class="built_in">String</span>)</span></span><br><span class="line">name!: string</span><br><span class="line"></span><br><span class="line"><span class="javascript">@Prop(&#123; <span class="attr">type</span>: <span class="built_in">Number</span> &#125;)</span></span><br><span class="line">age!: number</span><br><span class="line"></span><br><span class="line"><span class="javascript">@Prop(&#123; <span class="attr">type</span>: <span class="built_in">Number</span>, <span class="attr">default</span>: <span class="number">1</span> &#125;)</span></span><br><span class="line">sex!: number</span><br><span class="line"></span><br><span class="line">Sex: any = SEX</span><br><span class="line"><span class="javascript"><span class="comment">//这就是我用Emit装饰器 装饰的remove函数在触发的时候向上抛了一个 on-remove 事件 其中默认参数就是你函数里的参数</span></span></span><br><span class="line"><span class="javascript">@Emit(<span class="string">&#x27;on-remove&#x27;</span>)</span></span><br><span class="line"><span class="javascript">remove(id: number): <span class="keyword">void</span> &#123;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">return</span> id;</span></span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">//然后父组件 还是用@on-remove 来接收 并且指定 函数</span><br><span class="line"><span class="tag">&lt;<span class="name">User</span> <span class="attr">v-for</span>=<span class="string">&quot;item  in userList&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;item.id&quot;</span> <span class="attr">v-bind</span>=<span class="string">&quot;item&quot;</span> @<span class="attr">on-remove</span>=<span class="string">&quot;remove&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">lang</span>=<span class="string">&quot;ts&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> &#123; Component, Vue, Watch &#125; <span class="keyword">from</span> <span class="string">&#x27;vue-property-decorator&#x27;</span>;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> User <span class="keyword">from</span> <span class="string">&#x27;../components/User.vue&#x27;</span>;</span></span><br><span class="line"></span><br><span class="line">interface Iuser&#123;</span><br><span class="line">  id: number;</span><br><span class="line">  name: string;</span><br><span class="line">  age: number;</span><br><span class="line">  sex?: number;</span><br><span class="line">&#125;</span><br><span class="line">@Component(&#123;</span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;About&#x27;</span>,</span></span><br><span class="line">  components: &#123;</span><br><span class="line">    User,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;)</span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">About</span> <span class="keyword">extends</span> <span class="title">Vue</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">  userList: <span class="built_in">Array</span>&lt;Iuser&gt; = [</span></span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span>, <span class="attr">sex</span>: <span class="number">0</span>, <span class="attr">id</span>: <span class="number">1</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">28</span>, <span class="attr">sex</span>: <span class="number">1</span>, <span class="attr">id</span>: <span class="number">2</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line"><span class="javascript">      name: <span class="string">&#x27;王五&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span>, <span class="attr">id</span>: <span class="number">3</span>,</span></span><br><span class="line">    &#125;,</span><br><span class="line">  ]</span><br><span class="line"><span class="javascript">  <span class="comment">//这里还是跟以前的写法一样</span></span></span><br><span class="line"><span class="javascript">  <span class="function"><span class="title">remove</span>(<span class="params">id: number</span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log(id, <span class="string">&#x27;----父组件&#x27;</span>);</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//如果你的@Emit 不写 你要抛的事件名称 那么 那就默认会传递 你@Emit装饰器 装饰的方法</span></span><br><span class="line">@Emit()</span><br><span class="line"><span class="function"><span class="title">changAge</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//父组件 这里就会默认接收 你装饰器装饰的方法 遵循驼峰</span></span><br><span class="line">&lt;User v-<span class="keyword">for</span>=<span class="string">&quot;item  in userList&quot;</span> :key=<span class="string">&quot;item.id&quot;</span> v-bind=<span class="string">&quot;item&quot;</span> @chang-age=<span class="string">&quot;remove&quot;</span>/&gt;</span><br></pre></td></tr></table></figure>
<h2 id="model装饰器"><a href="#model装饰器" class="headerlink" title="@model装饰器"></a>@model装饰器</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>主要用于父组件 在子组件的标签上 使用v-model的时候 我们用来接收值</span><br><span class="line"><span class="number">2.</span>这里 我们原来是用value去在props里接收的</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//父组件使用v-model传递的值的时候 我们需要使用@model来接收下</span></span><br><span class="line"> &lt;User v-model=<span class="string">&quot;firstName&quot;</span> /&gt;</span><br><span class="line"><span class="comment">//子组件需要用@Model装饰器 来接收一下 并且要把当你在子组件修改这个父组件传递过来的值的事件也要写上</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Vue</span></span>&#123;</span><br><span class="line">@Model(<span class="string">&#x27;setFullname&#x27;</span>, &#123; <span class="attr">type</span>: <span class="built_in">String</span> &#125;)</span><br><span class="line">firname!: string</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//然后在标签中显示 这里还是绑定value 然后当你触发input的事件的时候 需要指定一个参数e.target.value就是你修改的值</span></span><br><span class="line">&lt;input :value=<span class="string">&quot;firname&quot;</span> @input=<span class="string">&quot;changFullName&quot;</span>/&gt;</span><br><span class="line"><span class="comment">//这里需要向上抛 你用@Model接收父组件传递的值的时候 就定义的事件</span></span><br><span class="line">@Emit(<span class="string">&#x27;setFullname&#x27;</span>)</span><br><span class="line">changFullName(e: any): string &#123;</span><br><span class="line">  <span class="comment">//然后把把你修改的值 直接就返回回去 你父组件的v-model 自动就鉴定到了你@Model的时候 传递过来的值</span></span><br><span class="line">  <span class="keyword">return</span> e.target.value;</span><br><span class="line">&#125;    </span><br></pre></td></tr></table></figure>
<h2 id="VUEX中的TS-模块写法"><a href="#VUEX中的TS-模块写法" class="headerlink" title="VUEX中的TS 模块写法"></a>VUEX中的TS 模块写法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们需要安装 vuex-<span class="built_in">module</span>-decorators 这个第三方包 才能使用hoock的模式</span><br><span class="line"><span class="number">2.</span>我们的Mutation 和 Action 就可以从这个依赖包离引了</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先使用 Module 定义模块 这个从 vuex-<span class="built_in">module</span>-decorators 导入 设置他的名字 并且 把你<span class="keyword">new</span> 好的store 挂载到这个配置项上</span><br><span class="line"><span class="number">2.</span>然后 我们要继承 VuexModule 这个模块 并且  这里可以使用接口的形式 </span><br><span class="line"><span class="number">3.</span>然后把我们定义好的这个模块 导入到我们的store里文件里面去 </span><br><span class="line"><span class="number">4.</span>这里 也需要 使用 getModule 把这个模块导入 为了能在页面中使用</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;Module,VuexModule,Mutation,Action,getModule &#125; <span class="keyword">from</span> <span class="string">&#x27;vuex-module-decorators&#x27;</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;./index&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//定义模块的类的接口  主要是定义state 定义你这个模块的类型 然后导入 方便我们在vuex里的index.ts文件 在定义泛型接口</span></span><br><span class="line"><span class="keyword">export</span> interface IAboutState&#123;</span><br><span class="line">    cont:number,</span><br><span class="line">    list:<span class="built_in">Array</span>&lt;number&gt;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//然后使用装饰器 定义模块 </span></span><br><span class="line">@Module(&#123;</span><br><span class="line">    <span class="comment">//模块的名称</span></span><br><span class="line">    name:<span class="string">&#x27;about&#x27;</span>,</span><br><span class="line">    dynamic:<span class="literal">true</span>,  <span class="comment">//表示这是一个动态的模块</span></span><br><span class="line">    store <span class="comment">//然后需要把这个模块挂载到store上</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//使用接口 也就是说你这about模块 </span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">About</span> <span class="keyword">extends</span> <span class="title">VuexModule</span> <span class="title">implements</span>  <span class="title">IAboutState</span></span>&#123;</span><br><span class="line">    cont:number = <span class="number">1</span></span><br><span class="line">    list:<span class="built_in">Array</span>&lt;number&gt; = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">    <span class="comment">//在写个计算属性</span></span><br><span class="line">    <span class="keyword">get</span> <span class="title">filterlist</span>()&#123;</span><br><span class="line">        <span class="comment">//这里 就以使用this 而不是使用 state.list 去使用这个数组了</span></span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.list.filter( <span class="function"><span class="params">item</span> =&gt;</span> item % <span class="number">2</span> == <span class="number">0</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//mutation 写法</span></span><br><span class="line">    @Mutation</span><br><span class="line">    <span class="function"><span class="title">updateCount</span>(<span class="params">payload:number</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.cont += payload</span><br><span class="line">    &#125;</span><br><span class="line">    @Mutation</span><br><span class="line">    <span class="function"><span class="title">updateList</span>(<span class="params">payload:<span class="built_in">Array</span>&lt;number&gt;</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.list = payload</span><br><span class="line">    &#125;</span><br><span class="line">    @Action</span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">getList</span>(<span class="params">payload:<span class="built_in">Array</span></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> res = <span class="keyword">await</span> Ajax() <span class="comment">//这里去发送请求 </span></span><br><span class="line">    	<span class="comment">//这里不用在commit在去提交mutation 直接调用mutation装饰的函数就好 </span></span><br><span class="line">        <span class="built_in">this</span>.updateList(res)</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//最后为了可以在页面中使用 这个模块 在通过 getModule 去导出 你定义的这个类</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> about = getModule(About)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//然后 在 我们的 vuex 初始文件中导入这个模块的接口 在定义 泛型接口 让我们的vuex 做规范</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span>;</span><br><span class="line"></span><br><span class="line">Vue.use(Vuex);</span><br><span class="line"><span class="comment">//引入我们的模块的定义的类型</span></span><br><span class="line"><span class="keyword">import</span> &#123;IAboutState&#125; <span class="keyword">from</span> <span class="string">&#x27;./about&#x27;</span></span><br><span class="line"><span class="comment">//如果还有别的模块 我们就在下面的泛型接口接着定义类型</span></span><br><span class="line"><span class="comment">// import &#123;IAinfo&#125; from &#x27;./info&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//然后在定义泛型接口 让我们的Vuex 遵循我们的接口</span></span><br><span class="line">interface IRootStore &#123;</span><br><span class="line">  about:IAboutState,</span><br><span class="line">  <span class="comment">//这里的about 和 info 都是模块的名称</span></span><br><span class="line">  <span class="comment">// info:IAinfo</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//我们的vuex要使用我们上面的泛型的接口</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Vuex.Store&lt;IRootStore&gt;(&#123;</span><br><span class="line"></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//上面我们由于使用了 getModule 去导出了 所以 直接在页面文件上使用即可了</span></span><br><span class="line"><span class="keyword">import</span> &#123; about &#125; <span class="keyword">from</span> <span class="string">&#x27;./store/about&#x27;</span></span><br><span class="line">@component()</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="title">extendx</span> <span class="title">Vue</span></span>&#123;</span><br><span class="line">    <span class="comment">//这样就取到了 about模块里的count 值</span></span><br><span class="line">    <span class="keyword">get</span> <span class="title">count</span>()&#123;</span><br><span class="line">        <span class="keyword">return</span> about.count </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//方法的执行 直接使用模块里面的方法即可</span></span><br><span class="line">    <span class="function"><span class="title">add</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">//这里就直接调取了 你about模块里的经过mutaition装饰的updateCount方法</span></span><br><span class="line">        about.updateCount(<span class="string">&#x27;传入的参数&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//执行action 直接调用就好</span></span><br><span class="line">    <span class="function"><span class="title">creatd</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        about.getList()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="钩子函数的写法"><a href="#钩子函数的写法" class="headerlink" title="钩子函数的写法"></a>钩子函数的写法</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里还是跟我们以前一样的写法 还是在类里面直接写即可</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>Vue + Ts 实战</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2020/04/24/vue+ts%E5%AE%9E%E6%88%98/">http://example.com/2020/04/24/vue+ts%E5%AE%9E%E6%88%98/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2020-04-24</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-06-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/TypeScript/">TypeScript</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/04/26/vue3.0/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Vue 3.0</div></div></a></div><div class="next-post pull-right"><a href="/2020/04/23/TypeScript/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">TypeScript</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2020/04/23/TypeScript/" title="TypeScript"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-23</div><div class="title">TypeScript</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Vue-TypeScript%E5%AE%9E%E6%88%98"><span class="toc-text">Vue + TypeScript实战</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80"><span class="toc-text">基础</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%84%E6%96%87%E4%BB%B6%E6%96%87%E4%BB%B6%E9%87%8C%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">组文件文件里的写法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Component%E8%A3%85%E9%A5%B0%E5%99%A8"><span class="toc-text">@Component装饰器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-text">数据的定义</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">计算属性的写法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7%E7%9A%84set"><span class="toc-text">计算属性的set</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Watch%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">@Watch的写法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Prop%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">@Prop的写法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%88%B6%E7%BB%84%E4%BB%B6%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">父组件的写法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#User%E7%BB%84%E4%BB%B6%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">User组件的写法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Emit%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">@Emit的写法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#model%E8%A3%85%E9%A5%B0%E5%99%A8"><span class="toc-text">@model装饰器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#VUEX%E4%B8%AD%E7%9A%84TS-%E6%A8%A1%E5%9D%97%E5%86%99%E6%B3%95"><span class="toc-text">VUEX中的TS 模块写法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%92%A9%E5%AD%90%E5%87%BD%E6%95%B0%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">钩子函数的写法</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>